/*
 * Plus One
Given a number represented as an array of digits, plus one to the number.
 */
package com.xinpan.exercise;

public class PlusOne {
    public int[] plusOne(int[] digits) {
        // Start typing your Java solution below
        // DO NOT write main() function
        int i = digits.length-1;
        for(; i >= 0; i--)
        {
            if(digits[i] == 9)
            {
                digits[i] = 0;
                continue;
            }
            break;
        }
        if(i >= 0)
        {
            digits[i]++;
            return digits;
        }
        else
        {
            int[] newd = new int[digits.length+1];
            newd[0] = 1;
            for(i = 1; i < newd.length; i++)
                newd[i] = 0;
            return newd;
        }
        
    }
}
